<?php

namespace App\Models\Passengers;

use App\Trip\IdentityCode\IdentityCode;
use App\Trip\Passengers\Passengers;
use App\Trip\Traits\ModelDateTrait;
use App\Trip\Traits\PassengerAttributeTrait;
use Carbon\Carbon;
use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Passengers\Passenger
 *
 * @property int                             $id
 * @property string                          $user_id        用户id
 * @property string                          $passenger_id   乘客id
 * @property string                          $truename       乘客姓名
 * @property int                             $identity_type  证件类型 1 身份证
 * @property string                          $identity_code  证件号
 * @property string                          $birth_day      生日
 * @property int                             $passenger_type 乘客类型 1 成人 2 学生 3 儿童
 * @property string                          $mobile         手机号
 * @property int                             $gender         性别 0未知  1 男 2 女
 * @property int                             $verify_state   核验状态 1 已核验 2 未核验
 * @property int                             $delete_state   删除状态 1 已删除 0 正常
 * @property string|null                     $deleted_at     删除时间
 * @property string                          $invite_user_id 用户id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static Builder|Passenger newModelQuery()
 * @method static Builder|Passenger newQuery()
 * @method static Builder|Passenger query()
 * @method static Builder|Passenger whereBirthDay($value)
 * @method static Builder|Passenger whereCreatedAt($value)
 * @method static Builder|Passenger whereDeleteState($value)
 * @method static Builder|Passenger whereDeletedAt($value)
 * @method static Builder|Passenger whereGender($value)
 * @method static Builder|Passenger whereId($value)
 * @method static Builder|Passenger whereIdentityCode($value)
 * @method static Builder|Passenger whereIdentityType($value)
 * @method static Builder|Passenger whereInviteUserId($value)
 * @method static Builder|Passenger whereMobile($value)
 * @method static Builder|Passenger wherePassengerId($value)
 * @method static Builder|Passenger wherePassengerType($value)
 * @method static Builder|Passenger whereTruename($value)
 * @method static Builder|Passenger whereUpdatedAt($value)
 * @method static Builder|Passenger whereUserId($value)
 * @method static Builder|Passenger whereVerifyState($value)
 * @mixin Eloquent
 */
class Passenger extends Model
{
    use HasFactory, ModelDateTrait, PassengerAttributeTrait;

    /**
     * 核验状态  1 已核验 2 待核验 3 核验失败
     * 1 2 3
     */
    const DELETE_STATE_NORMAL   = 0;
    const DELETE_STATE_DELETED  = 1;
    const VERIFY_STATE_VERIFIED = 1;
    const VERIFY_STATE_WAITING  = 2;
    const VERIFY_STATE_FAILED   = 3;

    protected $table  = 'passengers';
    protected $hidden = ['deleted_at', 'created_at', 'updated_at', 'id'];
    protected $fillable
                      = [
            'user_id',
            'passenger_id',
            'truename',
            'identity_type',
            'identity_code',
            'passenger_type',
            'mobile',
            'verify_state',
            'delete_state',
            'deleted_at',
            'invite_user_id',
            'birth_day',
            'gender',
        ];

    public static function getPassengerTypeString(): string
    {
        return implode(', ', array_keys(Passengers::PASSENGER_TYPE_DESC));
    }

    public static function getAddPassengerTypeString(): string
    {
        $desc = Passengers::PASSENGER_TYPE_DESC;
        unset($desc[Passengers::PASSENGER_TYPE_CHILD2]);
        return implode(', ', array_keys($desc));
    }

    public static function getIdentityTypeString(): string
    {
        return implode(', ', array_keys(Passengers::IDENTITY_TYPE_DESC));
    }

    public function getGender()
    {
        return $this->getAttribute('gender');
    }

    /**
     * 用户id
     *
     * @return string
     */
    public function getUserId(): string
    {
        return $this->getAttribute('user_id');
    }

    /**
     * 获取 id
     *
     * @return  string
     */
    public function getPassengerId(): string
    {
        return $this->getAttribute('passenger_id');
    }

    /**
     * 是否被删除
     *
     * @return bool
     */
    public function isDeleted(): bool
    {
        return $this->getAttribute('delete_state') == self::DELETE_STATE_DELETED;
    }

    /**
     * 是否有效
     *
     * @return bool
     */
    public function isVerified(): bool
    {
        return $this->getVerifyState() == self::VERIFY_STATE_VERIFIED;
    }

    /**
     * 乘客状态
     *
     * @return int
     */
    public function getVerifyState(): int
    {
        return $this->getAttribute('verify_state');
    }

    /**
     * 是否有效
     *
     * @return bool
     */
    public function isVerifyFailed(): bool
    {
        return $this->getVerifyState() == self::VERIFY_STATE_FAILED;
    }

    /**
     * 是否待核验
     *
     * @return bool
     */
    public function isWaitingVerify(): bool
    {
        return $this->getVerifyState() == self::VERIFY_STATE_WAITING;
    }

    public function getAge(): int
    {
        return Carbon::now()->diffInYears($this->getBirthDay());
    }

    public function getBirthDay(): string
    {
        return $this->getAttribute('birth_day');
    }

    /**
     * 通过 truename 属性构造 first_name  属性
     * @return Attribute
     */
    public function firstName(): Attribute
    {
        return Attribute::make(
            get: function ($value) {
                $split = "·";
                $names = explode($split, $this->truename);
                return $names[1] ?? '';
            },
        )->shouldCache();
    }

    public function getTrueName(): string
    {
        return $this->getAttribute('truename');
    }

    /**
     * 通过 birth_day 属性构造 age  属性
     * @return Attribute
     */
    protected function age(): Attribute
    {
        return Attribute::make(
            get: function ($value) {
                return Carbon::now()->diffInYears($this->getBirthDay());
            },
        );
    }

    /**
     * 通过 truename 属性构造 last_name  属性
     * @return Attribute
     */
    protected function lastName(): Attribute
    {
        return Attribute::make(
            get: function ($value) {
                $split = "·";
                $names = explode($split, $this->truename);
                return $names[0] ?? '';
            },
        )->shouldCache();
    }

    /**
     * 隐藏的证件号 属性构造
     * @return Attribute
     */
    protected function hiddenIdentityCode(): Attribute
    {
        return Attribute::make(
            get: function ($value) {
                $idc = new IdentityCode($this->getIdentityCode());

                return $this->is_invite ? $idc->hide(2, 2) : $idc->hide(6, 2);
            },
        )->shouldCache();
    }

    /**
     * 是否是邀请 属性构造
     *
     * @return Attribute
     */
    protected function isInvite(): Attribute
    {
        return Attribute::make(
            get: fn($value) => $this->getInviteUserId() ? 1 : 0
        );
    }

    /**
     * 获取受邀用户id
     *
     * @return  string
     */
    public function getInviteUserId(): string
    {
        return $this->getAttribute('invite_user_id') ?: '';
    }
}
